#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import division, unicode_literals

import re
import time


def validate_customer_name(name):
    """ 验证客户名称是否有效 """
    if len(name) < 2:
        return False

    special_chars = r'\!@#$%^&*+?":,'
    for i in name:
        # 如果包含特殊字符
        if i in special_chars:
            return False
    else:
        return True


def validate_number(number):
    """ 验证编号是否有效 """
    _compile = re.compile(r'^[a-zA-Z0-9_\-]+$')
    if _compile.match(number):
        return True

    return False


def list_section(number):
    """ 截取list列表，每页显示50行 """
    number = int(number)
    if number == 1:
        return 0, 50

    _end = number * 50
    _start = (_end - 49)
    return _start, _end


def timestamp_to_time(timestamp):
    """ 将timestamptime转化成日期 """
    time_struct = time.localtime(timestamp)
    return time.strftime('%Y-%m-%d %H:%M:%S', time_struct)
